#include "ExampleScene.h"

#include <graphics/utils.h>
#include <assert.h>
#include <render/renderer.h>

ExampleScene::ExampleScene()
    : Yam3DScene()
    , m_camera(0)
    , m_mesh1(0)
    , m_mesh2(0)
    , m_texture(0)
    , m_program(0)
{
    // Create cool vertex and fragment shaders.
    QGLShader *vshader = new QGLShader(QGLShader::Vertex);
    char vShaderStr[] =
       "uniform mat4 matModelView;                                                   \n"
       "uniform mat4 matModelViewProj;                                               \n"
       "uniform mat4 matModelViewInverseTranspose;                                   \n"
       "attribute vec4 a_position;                                                   \n"
       "attribute vec3 a_normal;                                                     \n"
       "attribute vec2 a_texCoord;                                                   \n"
       "varying vec3 vNormalES;                                                      \n"
       "varying vec3 vViewVec;                                                       \n"
       "varying vec2 vTexCoord;                                                      \n"
       "void main()                                                                  \n"
       "{                                                                            \n"
       "   gl_Position = matModelViewProj * a_position;                              \n"
       "   vNormalES   = vec3(matModelViewInverseTranspose * vec4(a_normal, 0.0));   \n"
       "   vViewVec    = -vec3(matModelView * a_position);                           \n"
       "   vTexCoord    = a_texCoord;                           \n"
       "}                                                                            \n";
    vshader->compileSourceCode(vShaderStr);

    QGLShader *fshader = new QGLShader(QGLShader::Fragment);
    char fShaderStr[] =
       "uniform vec4  lightDir;                                                                                      \n"
       "varying vec3 vNormalES;                                                                                      \n"
       "varying vec2 vTexCoord;                                                                                      \n"
       "varying vec3 vViewVec;                                                                                       \n"
       "uniform sampler2D sTexture;                                                                                  \n"
       "void main()                                                                                                  \n"
       "{                                                                                                            \n"
       "    vec3 normal = normalize(vNormalES);                                                                      \n"
       "    // Soft diffuse                                                                                          \n"
       "    float diffuse = 0.5 + 0.5 * dot(lightDir.xyz, normal);                                                   \n"
       "    // Standard specular                                                                                     \n"
       "    float specular = pow(clamp(dot(reflect(-normalize(vViewVec), normal), lightDir.xyz),0.0, 1.0), 6.0);     \n"
       "                                                                                                             \n"
       "    gl_FragColor = texture2D( sTexture, vTexCoord ) * diffuse + vec4(0.9,0.9,0.9,1.0) * specular;           \n"
       "}                                                                                                            \n";
    fshader->compileSourceCode(fShaderStr);

    assert( m_program == 0 );
    m_program = new QGLShaderProgram();
    m_program->addShader(vshader);
    m_program->addShader(fshader);
    m_program->link();
    m_program->bind();


    // Create camera for our scene.
    assert( m_camera == 0 );
    m_camera = new graphics::Camera();
    m_camera->setTranslation( QVector3D(0,0,2.0f) );

    m_mesh1 = graphics::genSphere( 512, 0.5f );
    m_mesh2 = graphics::genSphere( 512, 0.5f );

    int scale = 8;
    m_texture = graphics::createChessTexture(64*scale,64*scale,1*scale,false);

    m_nonFilteredPass.setCamera(m_camera);
    m_nonFilteredPass.setShaderProgram(m_program);
    m_nonFilteredPass.setTexture(0,"sTexture",m_texture);
    m_nonFilteredPass.getSamplerState(0)->setFiltering(graphics::SamplerState::NEAREST, graphics::SamplerState::NEAREST);

    m_filteredPass.setCamera(m_camera);
    m_filteredPass.setShaderProgram(m_program);
    m_filteredPass.setTexture(0,"sTexture",m_texture);
    m_filteredPass.getSamplerState(0)->setFiltering(graphics::SamplerState::LINEAR, graphics::SamplerState::LINEAR);

    graphics::ShaderUniforms* uniforms = new graphics::ShaderUniforms();
    m_nonFilteredPass.setShaderUniforms(uniforms);
    m_filteredPass.setShaderUniforms(uniforms);
}

ExampleScene::~ExampleScene()
{
}

void ExampleScene::resize(int width, int height)
{
    m_camera->setPerspective(60.0f, float(width)/float(height), 0.5f, 3.5f);
    glViewport( 0,0, width, height);
}

void ExampleScene::update(float deltaTime)
{
    m_mesh1->setTranslation( QVector3D(0.6,0,0) );
    m_mesh1->setOrientation( m_mesh1->getOrientation()
         * (QQuaternion::fromAxisAndAngle(1,0,0,5*deltaTime)
         * QQuaternion::fromAxisAndAngle(0,1,0,6*deltaTime)
         * QQuaternion::fromAxisAndAngle(0,0,1,7*deltaTime)) );

    m_mesh2->setTranslation( QVector3D(-0.6,0,0) );
    m_mesh2->setOrientation( m_mesh1->getOrientation() );

    QVector4D lightDir =  m_camera->getView().inverted() * QVector4D(1.0f,1.0f,0.0f,0.0f);
    m_nonFilteredPass.getShaderUniforms()->setUniformValue("lightDir", lightDir.normalized() );
}

void ExampleScene::render() const
{
    render::Renderer renderer;

    // Render mesh1
    renderer.applyPass(&m_nonFilteredPass);
    renderer.render(m_mesh1);

    // Render mesh2
    renderer.applyPass(&m_filteredPass);
    renderer.render(m_mesh2);
}


